package com.hd.crdao.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.AccountDetailsAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.ToDayDetailsBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/8/18 0018
 */

public class AccountDetailsActivity extends BaseActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.account_details_head)
    ExpandHeadView mAccountDetailsHead;
    @BindView(R.id.listview)
    ListView mListView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    public String mBusinessId;
    int pageNo = 1;
    int pageSize =20;
    public TextView mCount;
    public TextView mSumPrice;
    public List<ToDayDetailsBean.ReceiListTodayBean>  mList = new ArrayList<>();
    public AccountDetailsAdapter mAccountDetailsAdapter;
    private Handler mHandler = new Handler();
    @Override
    protected int getLayoutId() {
        return R.layout.activity_account_details;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        View headView = View.inflate(UIUtils.getContext(), R.layout.account_details_head, null);
        mCount = (TextView) headView.findViewById(R.id.count);   //总笔数
        mSumPrice = (TextView) headView.findViewById(R.id.sumPrice);   //总金额
        TextView desc = (TextView) headView.findViewById(R.id.desc);
        desc.setText("今日累计收款");
        mListView.addHeaderView(headView,null,false);
    }

    @Override
    protected void initData() {
        mBusinessId = SPUtils.getString(Config.businessId, "");
//        loadDate();
    }



    @Override
    protected void initListener() {
        mAccountDetailsHead.getLeftArrowLL().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mAccountDetailsHead.getHeadViewRightText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(UIUtils.getContext(),HistoryDetailsActivity.class));
                finish();
            }
        });

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if(mList!=null){
                            mList.clear();
                        }
                        pageNo=1;
                      loadDate();
                    }
                });
            }
        });

        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                      pageNo++;
                        loadDate();
                    }
                });
            }
        });

        mListView.setOnItemClickListener(this);
        mRefreshLayout.autoRefresh();
    }


    private void loadDate() {
        mRefreshLayout.setLoadmoreFinished(false);
        OkHttpUtils.post().url(Constants.FINDRECEILISTTODAY_URL).addParams("carBusinessId",mBusinessId).addParams("sign",Constants.SIGN)
                .addParams("pageNo",pageNo+"").addParams("pageSize",pageSize+"").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if(mRefreshLayout.isRefreshing()){
                    mRefreshLayout.finishRefresh();
                }else if(mRefreshLayout.isLoading()){
                    mRefreshLayout.finishLoadmore();
                }
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                Gson gson = new Gson();
                ToDayDetailsBean toDayDetailsBean = gson.fromJson(response, ToDayDetailsBean.class);
                String success = toDayDetailsBean.getSuccess();
                String message = toDayDetailsBean.getMessage();
                int total = toDayDetailsBean.getTotal();
                if(TextUtils.equals("00",success)){
                    if(total != 0){
                        List<ToDayDetailsBean.ReceiListTodayBean> receiListToday = toDayDetailsBean.getReceiListToday();
                        int totalToday = toDayDetailsBean.getTotalToday();
                        double amountToday = toDayDetailsBean.getAmountToday();
                        mCount.setText(totalToday+"笔");
                        mSumPrice.setText("+"+amountToday);
                        mList.addAll(receiListToday);
                        mAccountDetailsAdapter = new AccountDetailsAdapter(mList);
                        mListView.setAdapter(mAccountDetailsAdapter);
                    }
                    if(total ==mList.size() ){
                        mRefreshLayout.setLoadmoreFinished(true);
                    }
                }else {
                    MyToast.show(message);
                }
                if(mRefreshLayout.isRefreshing()){
                    mRefreshLayout.finishRefresh();
                }else if(mRefreshLayout.isLoading()){
                    mRefreshLayout.finishLoadmore();
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        position = position - mListView.getHeaderViewsCount();
        ToDayDetailsBean.ReceiListTodayBean item = (ToDayDetailsBean.ReceiListTodayBean) mAccountDetailsAdapter.getItem(position);
        Intent intent = new Intent(UIUtils.getContext(),DetailsActicity.class);
        intent.putExtra("PrName",item.getProduct().getPrName());
        intent.putExtra("id",item.getId());
        intent.putExtra("amount",item.getRlAmount()+"");
        intent.putExtra("time",item.getRlResTimeEnd());
        intent.putExtra("rate",item.getRlRate());
        intent.putExtra("rlT0",item.getRlT0());
        startActivity(intent);
    }
}
